package edu.nf.ch01.controller;

import edu.nf.ch01.common.MinioUtils;
import edu.nf.ch01.vo.ResultVO;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName UploadController
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/4 19:26
 * @Version 1.0
 */
@RestController
public class UploadController extends BaseController{

    @PostMapping("/upload/{path}")
    public ResultVO upload(@PathVariable("path") String path, @RequestParam("file") MultipartFile[] files) throws IOException {
        // 创建桶
        if (!MinioUtils.hasBucket("myapp")) {
            MinioUtils.createBucket("myapp");
        }

        for (MultipartFile file : files) {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件输入流
            InputStream inputStream = file.getInputStream();
            // 获取文件类型
            String contentType = file.getContentType();
            // 获取文件大小
            long size = file.getSize();
            MinioUtils.upload("myapp", path + "/" + fileName, contentType, size, inputStream);
        }
        return success();
    }
}
